﻿using System.Threading.Tasks;
using ZSoft.PushServices.Net.Transport;
using ZSoft.PushServices.Net.Transport.Hubs;

namespace ZSoft.PushServices.Server
{
    public class ChatHub: HubBase
    {
        private static int clientCount = 0;
        public void BroadCast(ChatMessage message)
        {
            message.Body = message.Body + string.Format(" (over <b style=\"color:red\">{0}</b> Protocol) [Connected clients : {1}] ", Context.QueryString["transport"], clientCount);
            Clients.All.Update(message);
        }

        public override Task OnConnected()
        {
            clientCount++;
            return base.OnConnected();
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            if (clientCount > 0)
            {
                clientCount--;
            }
            return base.OnDisconnected(stopCalled);
        }
    }
}